package leetcode.Y2024M5D23;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class S128 {
    public int longestConsecutive(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int res = 0;
        for (int num : nums) {
            if (map.containsKey(num)) continue;
            int lLen = map.getOrDefault(num - 1, 0);
            int rLen = map.getOrDefault(num + 1, 0);
            int len = lLen + rLen + 1;
            map.put(num, len);
            map.put(num - lLen, len);
            map.put(num + rLen, len);
            res=Math.max(res,len);
        }
        return res;
    }

    @Test
    public void test() {
        int[] nums = {100, 4, 200, 1, 3, 2};
        System.out.println(longestConsecutive(nums));
        int[] nums1 = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1};
        int[] nums2 = {1,2,0,1};
        System.out.println(longestConsecutive(nums1));
        System.out.println(longestConsecutive(nums2));
    }
}
